package com.mhfelipe.android.bancodedados;

import android.app.Activity;
import android.app.AlertDialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class BancoDeDadosActivity extends Activity {
    /** Called when the activity is first created. */

	private SQLiteDatabase database;

	private Cursor cursor;

	private EditText nomeEditText;

	private TextView nomeTextView;

	private Button cadastroButton, consultaButton, voltarButton, salvarButton,
			primeiroButton, anteriorButton, proximoButton, ultimoButton;

	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		database.close();
	}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        openMain();
        getDatabase();
    }

    private void salvar() {
    	String sql = "insert into contato(nome) values (?)";
		Object[] params = new Object[] { nomeEditText.getText().toString() };
		database.execSQL(sql, params);
//    	String sql = "insert into contato(nome) values ('" + nomeEditText.getText().toString() + "')";
//		database.execSQL(sql);
    }

    private void buscarDados() {
    	String table = "contato";
		String[] columns = new String[] {"nome"};
		String selection = null;
		String[] selectionArgs = null;
		String groupBy = null;
		String having = null;
		String orderBy = null;
		cursor = database.query(
    			table,
    			columns,
    			selection,
    			selectionArgs,
    			groupBy,
    			having,
    			orderBy
		);

		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
		}
    }

    private void mostrarDados() {
    	nomeTextView.setText(cursor.getString(0));
    }

    private void mostrarRegistroAnterior() {
    	cursor.moveToPrevious();
    	mostrarDados();
    }

    private void mostrarProximoRegistro() {
    	cursor.moveToNext();
    	mostrarDados();
    }

    private void openMain() {
        setContentView(R.layout.main);
        init();
    	cadastroButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				openCadastro();
			}
		});
    	consultaButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				openConsulta();
			}
		});
	}

	private void openCadastro() {
        setContentView(R.layout.cadastro);
        init();
        voltarButton.setOnClickListener(new View.OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		openMain();
        	}
        });
        salvarButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				salvar();
				message("Aviso", "Registro salvo com sucesso.");
				nomeEditText.setText("");
			}
		});
	}

	private void openConsulta() {
        setContentView(R.layout.consulta);
        init();
        buscarDados();
        mostrarDados();
        voltarButton.setOnClickListener(new View.OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		openMain();
        	}
        });
        anteriorButton.setOnClickListener(new View.OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		mostrarRegistroAnterior();
        	}
        });
        proximoButton.setOnClickListener(new View.OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		mostrarProximoRegistro();
        	}
        });
	}

	private void init() {
    	cadastroButton = (Button) findViewById(R.button.cadastro);
    	consultaButton = (Button) findViewById(R.button.consulta);
    	salvarButton = (Button) findViewById(R.button.salvar);
    	voltarButton = (Button) findViewById(R.button.voltar);

    	primeiroButton = (Button) findViewById(R.button.primeiro);
    	anteriorButton = (Button) findViewById(R.button.anterior);
    	proximoButton = (Button) findViewById(R.button.proximo);
    	ultimoButton = (Button) findViewById(R.button.ultimo);

    	nomeEditText = (EditText) findViewById(R.editText.contato);
    	nomeTextView = (TextView) findViewById(R.textView.contato);
	}

	private void getDatabase() {
    	database = openOrCreateDatabase("dbName", MODE_WORLD_READABLE, null);
    	String sql = "create table if not exists contato (" +
    			"id integer primary key," +
    			"nome text" +
    			");";
		database.execSQL(sql);
    }

    private void message(String title, String message) {
    	AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    	dialogBuilder.setTitle(title);
    	dialogBuilder.setMessage(message);
    	dialogBuilder.setNeutralButton("OK", null);
    	dialogBuilder.show();
    }

}